package org.example.view.normal;

import org.example.controller.AdminController;
import org.example.data.Result;
import org.example.data.RoomIn;
import org.example.data.UserWithDetails;
import org.example.view.Login;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.ParseException;

public class NormalIndex extends JFrame {

    private JTextField txtRoomId;
    private JTextField txtCheckInTime;
    private JTextField txtCheckoutTime;
    private JButton btnCheckIn;
    private JButton btnCheckOut;
    private UserWithDetails user;

    public NormalIndex(UserWithDetails user) {
        super("办理入住与退房");
        this.user = user;
        // 初始化UI组件
        JLabel lblRoomId = new JLabel("房间ID:");
        txtRoomId = new JTextField(10);
        JLabel lblCheckInTime = new JLabel("入住时间(yyyy-MM-dd):");
        txtCheckInTime = new JTextField(20);
        JLabel lblCheckoutTime = new JLabel("退房时间(yyyy-MM-dd):");
        txtCheckoutTime = new JTextField(20);
        btnCheckIn = new JButton("办理入住");
        btnCheckOut = new JButton("办理退房");
        JButton exit = new JButton("退出");
        exit.addActionListener(e -> {
            SwingUtilities.invokeLater(() -> new Login().setVisible(true));
            dispose();
        });

        // 布局设置
        JPanel panel = new JPanel(new GridLayout(5, 2));
        panel.add(lblRoomId);
        panel.add(txtRoomId);
        panel.add(lblCheckInTime);
        panel.add(txtCheckInTime);
        panel.add(lblCheckoutTime);
        panel.add(txtCheckoutTime);
        panel.add(btnCheckIn);
        panel.add(btnCheckOut);
        panel.add(exit);


        add(panel);

        // 办理入住按钮事件监听
        btnCheckIn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                handleCheckIn();
            }
        });

        // 办理退房按钮事件监听
        btnCheckOut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                handleCheckOut();
            }
        });

        // 设置窗口属性
        setSize(400, 400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void handleCheckIn() {
        String roomId = txtRoomId.getText();
        String checkInStr = txtCheckInTime.getText();
        String checkoutStr = txtCheckoutTime.getText();
        try {
            RoomIn roomIn = new RoomIn(Integer.valueOf(roomId),user.getUser().getId(),checkInStr,checkoutStr);
            Result<RoomIn> result = new AdminController().checkIn(roomIn);
            if (result.getStatus()) {
                JOptionPane.showMessageDialog(this, "入住成功");
            } else {
                JOptionPane.showMessageDialog(this, "入住失败，请重试");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private void handleCheckOut() {
        String roomId = txtRoomId.getText();
        try {
            RoomIn roomIn = new RoomIn(Integer.valueOf(roomId));
            Result<Boolean> result = new AdminController().checkOut(roomIn);
            if (result.getStatus()) {
                JOptionPane.showMessageDialog(this, "退房成功");
            } else {
                JOptionPane.showMessageDialog(this, result.getMessage());
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
